package com.acce.product.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.acce.base.pojo.ArgsPojo;
import com.alibaba.fastjson.JSONObject;

public abstract class BaseController {

	private Logger logger = Logger.getLogger(BaseController.class);
	
	/**
	 * 将business json串根据serviceCode转换成对应业务对象
	 * @param business
	 */
	public boolean checkBaseArgs(ArgsPojo args) {
		logger.info("nsy-product::checkBaseArgs:: base args: " + JSONObject.toJSONString(args));
		if(args == null
			|| StringUtils.isEmpty(args.getServiceCode())
			|| StringUtils.isEmpty(args.getSign())
			|| StringUtils.isEmpty(args.getTimestamp())
			|| StringUtils.isEmpty(args.getNonce())
			|| StringUtils.isEmpty(args.getDevice())
			|| StringUtils.isEmpty(args.getIpaddr())
			|| StringUtils.isEmpty(args.getMac())
			|| StringUtils.isEmpty(args.getEditUserId())
			|| StringUtils.isEmpty(args.getEditTime())
			|| StringUtils.isEmpty(args.getBusiness())) {
			logger.info("nsy-product::checkBaseArgs::miss args!");
			return false;
		}
		logger.info("nsy-product::checkBaseArgs::args matches!");
		return true;
	}
	
	public void writeJsonToResponse(HttpServletResponse response, String json) {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.println(json);
			writer.flush();
		} catch (IOException e) {
			logger.error("return json error:" + json,e);
		} finally {
			if(null != writer) {
				writer.close();
			}
		}
	}
}
